package com.cn.xiongmeng.czlxz.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.xiongmeng.czlxz.R;

import org.json.JSONException;
import org.json.JSONObject;

public class AvatarFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "AvatarFragment";
    private TextView tvUid;
    public AvatarFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach");
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_avatar, container, false);

        ImageView btnOff = rootView.findViewById(R.id.btn_off);
        tvUid = rootView.findViewById(R.id.uid);
        TextView tvProposal = rootView.findViewById(R.id.tv_proposal);
        TextView tvPrivacy = rootView.findViewById(R.id.tv_privacy);

        btnOff.setOnClickListener(this);
        tvUid.setOnClickListener(this);
        tvProposal.setOnClickListener(this);
        tvPrivacy.setOnClickListener(this);

        setUid();

        return rootView;
    }
    @Override
    public void onClick(View v){
        if (v.getId() == R.id.btn_off){
            Log.d(TAG, "点击叉号");

            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            // 隐藏Fragment
            if (this.isAdded()) {
                fragmentTransaction.hide(this);
            }
            fragmentTransaction.commit();
        } else if (v.getId() == R.id.uid) {
            Log.d(TAG, "点击uid");

        } else if (v.getId() == R.id.tv_proposal) {
            Log.d(TAG, "点击用户协议");
            openTermsUrl();

        } else if (v.getId() == R.id.tv_privacy) {
            Log.d(TAG, "点击隐私政策");
            openPrivacyUrl();
        }
    }
    private void setUid(){
        SharedPreferences prefs = getActivity().getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
        String fakeUser = prefs.getString("fakeUser", "");

        if (fakeUser.length() != 0){
            try {
                JSONObject jsonObject = new JSONObject(fakeUser);
                String fakeUid = jsonObject.getString("fake_uid");
                tvUid.setText(fakeUid);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    private void openTermsUrl() {
        // 使用 Intent 打开用户协议网页
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://little-moon.fun/terms"));
        startActivity(intent);
    }
    private void openPrivacyUrl() {
        // 使用 Intent 打开隐私政策网页
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://little-moon.fun/privacy"));
        startActivity(intent);
    }
}